// Main header
#include "../Main.h"

// My header
#include "Controller_ModalAddWidgetDialog.h"

// Fridge
#include "Controller_Main.h"
#include "../Preset/Element.h"
#include "../Preset/ExposedVariable.h"
#include "../Widgets/Button.h"
#include "../Widgets/DropdownList.h"
#include "../Widgets/DialogPanel.h"
#include "../Widgets/StaticText.h"
#include "../Widgets/TextEditor.h"
#include "../Widgets/Tickbox.h"

static const u_int uDIALOG_BUTTON_HEIGHT = 32;
static const u_int uDIALOG_BUTTON_WIDTH = 128;
static const u_int uDIALOG_MARGIN = 24;

DialogPanel*			Controller_ModalAddWidgetDialog::s_pxDialogPanel = 0;
TextEditor*				Controller_ModalAddWidgetDialog::s_pxTextEditorName = 0;
bool					Controller_ModalAddWidgetDialog::s_bDestroy = false;
const char*				Controller_ModalAddWidgetDialog::s_szName = 0;
bool					Controller_ModalAddWidgetDialog::s_bShowLabel = true;
EXPOSED_VARIABLE_TYPE	Controller_ModalAddWidgetDialog::s_xControlType = EXPOSED_VARIABLE_NUMBER;
bool					Controller_ModalAddWidgetDialog::s_bShowScale = true;
bool					Controller_ModalAddWidgetDialog::s_bShowMinMaxLabels = true;
float					Controller_ModalAddWidgetDialog::s_fSliderMin = 0.0f;
float					Controller_ModalAddWidgetDialog::s_fSliderMax = 1.0f;

void( * Controller_ModalAddWidgetDialog::s_pfnCallback )( const Controller_ModalDialogResult ) = NULL;

void Controller_ModalAddWidgetDialog::Initialise()
{
}

void Controller_ModalAddWidgetDialog::Update()
{
	if( s_bDestroy )
	{
		Destroy();
	}
}

void Controller_ModalAddWidgetDialog::Shutdown()
{
	Controller_Main::RemoveModal( s_pxDialogPanel );
	delete s_pxDialogPanel;
	s_pxDialogPanel = 0;
}

void Controller_ModalAddWidgetDialog::Show()
{
	if( s_pxDialogPanel )
	{
		return;
	}
	
	s_pxDialogPanel = new DialogPanel( Controller_Main::GetWindowPanel() );
	Controller_Main::AddModal( s_pxDialogPanel );
	
	u_int uTextFieldHeight = 24;
	u_int uLabelXPosition = s_pxDialogPanel->GetOriginalX() + uDIALOG_MARGIN / 2;
	u_int uVerticalPos = s_pxDialogPanel->GetOriginalY() + uDIALOG_MARGIN;
	
	// TextEdit - Name 
	StaticText* pxLabelName = new StaticText( s_pxDialogPanel );
	pxLabelName->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
	pxLabelName->SetAutoSizes( true );
	pxLabelName->SetText( "Name:" );
	pxLabelName->SetPosition( uLabelXPosition, uVerticalPos );
	uVerticalPos += 17;
	
	s_pxTextEditorName = new TextEditor( s_pxDialogPanel );
	s_pxTextEditorName->SetPosition( uLabelXPosition, uVerticalPos );
	s_pxTextEditorName->SetSize( 200, uTextFieldHeight );
    s_pxTextEditorName->SetAnchors( ANCHOR_BOTTOM | ANCHOR_LEFT | ANCHOR_RIGHT );
	s_pxTextEditorName->SetHasBorder( true );
	s_pxTextEditorName->SetBorderWidth( 1.0f );
	s_pxTextEditorName->SetSingleLine( true );
	uVerticalPos += 34;

	
	// Tickbox - Show Label
	Tickbox* pxTickboxShowLabel = new Tickbox( s_pxDialogPanel );
	pxTickboxShowLabel->SetPosition( uLabelXPosition, uVerticalPos );
	pxTickboxShowLabel->SetLabel( "Show Label" );
	pxTickboxShowLabel->SetBool( &s_bShowLabel );
	uVerticalPos += 24;
	
	// Dropdown - Control Type
	StaticText* pxLabelControlType = new StaticText( s_pxDialogPanel );
	pxLabelControlType->SetPosition( uLabelXPosition, uVerticalPos );
	pxLabelControlType->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
	pxLabelControlType->SetAutoSizes( true );
	pxLabelControlType->SetText( "Control Type:" );
	uVerticalPos += 17;
	
	DropdownList* pxControlTypeDropdown = new DropdownList( s_pxDialogPanel );
	pxControlTypeDropdown->SetPosition( uLabelXPosition, uVerticalPos );
	pxControlTypeDropdown->SetSize( 200, uTextFieldHeight );
	pxControlTypeDropdown->SetAnchors( ANCHOR_BOTTOM | ANCHOR_LEFT | ANCHOR_RIGHT );
	pxControlTypeDropdown->AddListItem( "Number", ControlTypeCallback_Number, 0 );
	pxControlTypeDropdown->AddListItem( "Slider", ControlTypeCallback_Slider, 0 );
	pxControlTypeDropdown->AddListItem( "Color",  ControlTypeCallback_Color,  0 );
	uVerticalPos += 38;
	
	s_pxDialogPanel->SetSize( 500, 300 );
    s_pxDialogPanel->SetPosition( MIN_SCREEN_WIDTH / 2 - s_pxDialogPanel->GetOriginalWidth() / 2, MIN_SCREEN_HEIGHT / 2 - s_pxDialogPanel->GetOriginalHeight() / 2  );
    s_pxDialogPanel->SetColour( 0xffffffff );
    s_pxDialogPanel->SetHasBorder( true );
	s_pxDialogPanel->SetHasMoveBar( true );
	
	// OK / Cancel Buttons
    Button* pxButton = NULL;

	pxButton = new Button( s_pxDialogPanel );
	pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
	pxButton->SetLabel( "OK" );
	pxButton->SetPosition( s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 - uDIALOG_BUTTON_WIDTH - uDIALOG_MARGIN / 2,
						   s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
						 );
	pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
	pxButton->SetButtonPressCallback( OKCallback, NULL );
	
	pxButton = new Button( s_pxDialogPanel );
	pxButton->SetAnchors( ANCHORS_ALL_PARENT_RELATIVE );
	pxButton->SetLabel( "Cancel" );
	pxButton->SetPosition( s_pxDialogPanel->GetOriginalX() + s_pxDialogPanel->GetOriginalWidth() / 2 + uDIALOG_MARGIN / 2,
						   s_pxDialogPanel->GetOriginalY() + s_pxDialogPanel->GetOriginalHeight() - ( uDIALOG_MARGIN + uDIALOG_BUTTON_HEIGHT )
						 );
	pxButton->SetSize( uDIALOG_BUTTON_WIDTH, uDIALOG_BUTTON_HEIGHT );
	pxButton->SetButtonPressCallback( CancelCallback, NULL );
}

void Controller_ModalAddWidgetDialog::ControlTypeCallback_Number( void* pUserData )
{
	s_xControlType = EXPOSED_VARIABLE_NUMBER;
}

void Controller_ModalAddWidgetDialog::ControlTypeCallback_Slider( void* pUserData )
{
	s_xControlType = EXPOSED_VARIABLE_SLIDER;
}

void Controller_ModalAddWidgetDialog::ControlTypeCallback_Color( void* pUserData )
{
	s_xControlType = EXPOSED_VARIABLE_COLOUR;
}

void Controller_ModalAddWidgetDialog::OKCallback( void* pUserData )
{
	s_szName = s_pxTextEditorName->GetText();
	
	// Check for no name
	if( !s_szName || !s_szName[ 0 ] )
	{
		Controller_ModalDialog::Show( "Please enter a name for the control.", DIALOGTYPE_OKONLY );
		return;
	}
	
	if( Controller_Screen_Editor::GetControlByName( s_szName ) )
	{
		Controller_ModalDialog::Show( "There is already a controller with that name.", DIALOGTYPE_OKONLY );
		return;
	}
	
	s_bDestroy = true;
	
    if( s_pfnCallback )
    {
        s_pfnCallback( DIALOGRESULT_OK );
    }
}

void Controller_ModalAddWidgetDialog::CancelCallback( void* pUserData )
{		
    if( s_pfnCallback )
    {
		s_pfnCallback( DIALOGRESULT_CANCEL );
    }
	
	s_bDestroy = true;
}

void Controller_ModalAddWidgetDialog::Destroy()
{
	if( s_pxDialogPanel )
	{
		Controller_Main::RemoveModal( s_pxDialogPanel );
		delete s_pxDialogPanel;
		s_pxDialogPanel = 0;
	}
	s_bDestroy = false;
}

// eof
